<?php
//+----------------------------------------------------
//| 接口类: 提供登录相关接口
//| Author: web_dxj@163.com
//+----------------------------------------------------

namespace Android\Controller;

class LoginController extends BaseController {


	public function index() {
        // 输出十分钟后的时间
		echo date('Y-m-d H:i:s',strtotime("+10 minute")); 
	}


	/**
	 * 客户端登录接口 (该接口可以使用GET或者POST方式提交)
	 * 
	 * @param  string $account      登录账号
	 * @param  string $password     登录密码
     * @param  string $role         登录角色
     * @param  string $client_id    极光客户端ID
	 * @return string               返回json句柄
	 * 
	 */
	public function login() {

		$account = I ('request.account','');
        $password = I ('request.password','');
        $role = I ('request.role','');
        $client_id = i ('request.client_id'); // 极光推送客户端生成的ClientId

        if (empty($account) || empty($password) || empty($role))
            $this->out($this->fail,'Some form item is nothing');

        switch (strtoupper($role)) {
            case 'STUDENT':
                $this->studentLogin($account,$password,$client_id);
                break;
            case 'TEACHER':
                $this->teacherLogin($account,$password,$client_id);
                break;
            default :
                $this->out($this->fail,'Underfine role');
        }
	}

    public function logout() {

        $role = I ('request.role',null);
        $id = I ('request.id',null);

        if (empty($role) || empty($id))
            $this->out($this->fail,'Empty parameter');

        switch (strtoupper($role)) {
            case 'TEACHER':
                $flag = M ('JpushMapping2')->where('tid = '.$id)->delete();
                break;
            case 'STUDENT':
                $flag = M ('JpushMapping')->where('sid = ' .$id)->delete();
                break;
        }
        if ($flag === false)
            $this->out($this->fail,'退出失败');
        $this->out($this->success,'退出成功');
    }

    /**
     * 学生登录方法
     *
     * @param $account
     * @param $password
     * @param $client_id
     */
    protected function studentLogin($account,$password,$client_id) {

        $Student = D ('Student');
        if ($Student->isExist($account) === false) {
            $this->out($this->fail,'学生不存在');
        }

        $stu = $Student->login($account,$password);
        if ($stu === false) {
            $this->out($this->fail, '密码错误');
        }

        $stu['department_name'] = M ('Department')->where('id = '.$stu['department_id'])->getField('name');
        $stu['profession_name'] = M ('Profession')->where('id = '.$stu['profession_id'])->getField('name');
        $stu['class_name'] = M ('Class')->where('id ='.$stu['class_id'])->getField('name');

        // 更新client_id
        $flag = D ('JpushMapping')->refresh($stu,$client_id);
        if ($flag === false) {
            $this->out($this->fail,'Refresh jpush mapping fail');
        }

        // 记录学生登录
        $this->saveVisit(0,$stu['id']);
        $this->out($this->success,'登录成功',$stu);
    }


    /**
     * 教师登录方法
     *
     * @param $account
     * @param $password
     * @param $client_id
     */
    protected function teacherLogin($account,$password, $client_id) {

        $Teacher = D ('Teacher');
        if ($Teacher->isExist($account) === false) {
            $this->out($this->fail,'教师不存在');
        }

        $tea = $Teacher->login($account,$password);
        if ($tea === false) {
            $this->out($this->fail,'密码错误');
        }

        $tea['department_name'] = M ('Department')->where('id = '.$tea['department_id'])->getField('name');

        // 更新client_id
        $flag = D ('JpushMapping2')->refresh($tea,$client_id);
        if ($flag === false) {
            $this->out($this->fail,'Refresh jpush mapping fail');
        }

        // 记录教师登录
        $this->saveVisit(1,$tea['id']);

        $this->out($this->success,'登录成功',$tea);
    }


    /**
     * 保存访问记录
     * integer $role 0 学生 1 教师
     * integer $uid 用户id
     */
    protected function saveVisit($role,$uid) {

        $ip = get_client_ip();
        $time = time();

        $Visit = M ('Visit');
        $map['ip'] = $ip;
        $map['role'] = $role;
        $v = $Visit->where($map)->order('time desc')->find();

        if (is_null($v)) {
            $data['ip'] = $ip;
            $data['time'] = date('Y-m-d H:i:s',$time);
            $data['view'] = 1;
            $data['uid'] = $uid;
            $data['role'] = $role;

            $data['y'] = date('Y',$time);
            $data['m'] = date('m',$time);
            $data['d'] = date('d',$time);
            $Visit->add($data);
            unset($data);
        } else {
            $id = $v['id'];
            $lastTime = $v['time'];

            $y = date('Y',strtotime($lastTime));
            $m = date('m',strtotime($lastTime));
            $d = date('d',strtotime($lastTime));
            $y_ = date('Y',$time);
            $m_ = date('m',$time);
            $d_ = date('d',$time);

            if ($y == $y_ && $m == $m_ && $d == $d_) {
                //用户当天访问量加1
                $Visit->where('id = '.$id)->setInc('view');
            } else {
                $data['ip'] = $ip;
                $data['time'] = date('Y-m-d',$time);
                $data['view'] = 1;
                $data['uid'] = $uid;
                $data['role'] = $role;
                $data['y'] = $y_;
                $data['m'] = $m_;
                $data['d'] = $d_;
                $Visit->add($data);
            }
        }
    }


}